// pages/t_login/t_login.js
import request from '../../request/request.js'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    password: ''
  },
  onLoad: function (options) {
    let coachInfo = wx.getStorageSync("coachInfo");
    if(coachInfo!==''){
      wx.navigateTo({
        url: '../t_index/t_index',
      })
    }
  },
  onBlurName(e) {
    this.setData({ userName: e.detail.value })
  },
  onBlurPwd(e) {
    this.setData({ password: e.detail.value })
  },
  login() {
    var that = this;
    if (!this.data.userName || !this.data.password) {
      wx.showToast({
        title: '请输入用户名或密码',
        icon: 'none',
        duration: 1500,
        mask: true
      });
      return;
    }
    request({
      url: 'coach/login',
      method: 'post',
      tag:'login',
      data: {
        username: that.data.userName,
        password: that.data.password
      }
    }).then(res => {
      if(res.data.code===200){
        console.log(res.data.data);
        wx.setStorageSync("coachInfo", res.data.data);
      }
      if(res.data.code===10001){
        wx.showToast({
          title: '用户名或密码错误',
          icon: 'none',
          duration: 1500,
          mask: true
        });
        return;
      }
      wx.navigateTo({
        url: '../t_index/t_index',
      })
    }).catch(err => {})
    
  }

})